<?php

class MD_Products extends ModelBase {

    function MD_Products(&$database) {
	$this->tableName = TABLE_PRODUCTS;
	$this->keyField = "id";
	ModelBase::ModelBase($database);
    }

    function update_type(&$arrayData) {
	$sql = "UPDATE  tbl_product_type SET ";
	$sql .= "type_name='" . $this->replaceString(&$arrayData["type_name"]) . "'";
	$sql .= ",type_note='" . $this->replaceString(&$arrayData["type_note"]) . "'";
	$sql .= ",type_parent='" . $this->replaceString(&$arrayData["type_parent"]) . "'";

	$sql .= ",type_detail='" . $this->replaceString(&$arrayData["type_detail"]) . "'";
	
	$sql .= " WHERE $this->keyField=" . $arrayData["type_id"];
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function update_product(&$arrayData) {
	$sql = "UPDATE  " . $this->tableName . " SET ";
	$sql .= "product_name='" . $this->replaceString(&$arrayData["product_name"]) . "'";
	$sql .= ",product_price='" . $this->replaceString(&$arrayData["product_price"]) . "'";
	$sql .= ",product_note='" . $this->replaceString(&$arrayData["product_note"]) . "'";
	$sql .= ",product_type_id='" . $this->replaceString(&$arrayData["product_type_id"]) . "'";
	if ($_FILES["product_img"]['name'] != "") {
	    $sql .= ",product_img='" . $_SESSION['Pkd_images1'] . "'";
	}
	$sql .= " WHERE $this->keyField=" . $arrayData["id"];
	$this->db->query($sql);
    }

    function insert_product(&$arrayData) {
	$listFilds .= "product_name";
	$listValues .= "'" . $this->replaceString(&$arrayData["product_name"]) . "'";
	$listFilds .= ",product_price";
	$listValues .= ",'" . $this->replaceString(&$arrayData["product_price"]) . "'";
	$listFilds .= ",product_note";
	$listValues .= ",'" . $this->replaceString(&$arrayData["product_note"]) . "'";
	$listFilds .= ",product_type_id";
	$listValues .= ",'" . $this->replaceString(&$arrayData["product_type_id"]) . "'";
	if ($_FILES["product_img"]['name'] != "") {
	    $listFilds .= ",product_img";
	    $listValues .= ",'" . $_SESSION['Pkd_images1'] . "'";
	}
	$sql = "INSERT INTO $this->tableName (  $listFilds ) ";
	$sql .= " VALUES ($listValues ); ";
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function insert_type(&$arrayData) {
	$listFilds .= "type_name";
	$listValues .= "'" . $this->replaceString(&$arrayData["type_name"]) . "'";
	$listFilds .= ",type_note";
	$listValues .= ",'" . $this->replaceString(&$arrayData["type_note"]) . "'";
	$listFilds .= ",type_parent";
	$listValues .= ",'" . $this->replaceString(&$arrayData["type_parent"]) . "'";

	$sql = "INSERT INTO tbl_product_type (  $listFilds ) ";
	$sql .= " VALUES ($listValues ); ";
//	echo $sql;
//	die();
	$this->db->query($sql);
    }

    function getById($id) {
	$sql = "SELECT *  FROM $this->tableName ";
	if ($id != "") {
	    $sql .= " WHERE $this->keyField =$id ";
	}
	return $this->db->retrieve($sql);
    }

    function get_product_type_by_Where($where, $order, $limit) {
	$sql = "SELECT *  FROM tbl_product_type";
	if ($where != "") {
	    $sql .= " WHERE $where ";
	}
	if ($order != "") {
	    $sql .= " ORDER BY $order ";
	}
	if ($limit != "") {
	    $sql .= " LIMIT $limit ";
	}
//	echo $sql."<br/>";
	return $this->db->retrieve($sql);
    }

    function get_product_type_by_Where_1($where, $order, $limit) {
	$sql = "SELECT *  FROM tbl_product_type";
	if ($where != "") {
	    $sql .= " WHERE $where ";
	}
	if ($order != "") {
	    $sql .= " ORDER BY $order ";
	}
	if ($limit != "") {
	    $sql .= " LIMIT $limit ";
	}
//	echo $sql."<br/>";
	return $this->db->getFirstRecord($sql);
    }

    function delete_type($id) {
	$sql = "DELETE FROM tbl_product_type WHERE $this->keyField =$id";
//	echo $sql;
	return $this->db->query($sql);
    }

    function delete_product($id) {
	$sql = "DELETE FROM $this->tableName WHERE $this->keyField =$id";
//	echo $sql;
	return $this->db->query($sql);
    }

}

?>